package com.ethink.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.web.context.SPRequestContextHolder;
import com.ethink.warehouse.api.enums.MateInventoryOtherOutLogOrderStatusEnum;
import com.ethink.warehouse.api.info.MateInventoryOtherOutLogDetailInfo;
import com.ethink.warehouse.api.info.MateInventoryOtherOutLogInfo;
import com.ethink.warehouse.api.info.MateInventoryOtherOutLogModeInfo;
import com.ethink.warehouse.api.info.SubWarehouseInfo;
import com.ethink.warehouse.api.request.MateInventoryOtherOutLogAddRequest;
import com.ethink.warehouse.api.request.MateInventoryOtherOutLogQueryRequest;
import com.ethink.warehouse.api.request.MateInventoryOtherOutLogUpdateRequest;
import com.ethink.warehouse.api.response.MateInventoryOtherOutLogAddResponse;
import com.ethink.warehouse.api.response.MateInventoryOtherOutLogDetailResponse;
import com.ethink.warehouse.api.response.MateInventoryOtherOutLogUpdateResponse;
import com.ethink.warehouse.common.entity.MateInventoryOtherOutLog;
import com.ethink.warehouse.common.mapper.MateInventoryOtherOutLogMapper;
import com.ethink.warehouse.converter.MateInventoryOtherOutLogConverter;
import com.ethink.warehouse.service.MateInventoryOtherOutLogDetailService;
import com.ethink.warehouse.service.MateInventoryOtherOutLogService;
import com.ethink.warehouse.service.MateInventoryService;
import com.ethink.warehouse.service.SubWarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 其他出库 Service接口实现
 *
 * @author default
 * @date 2023-02-08
 */
@Slf4j
@Service
public class MateInventoryOtherOutLogServiceImpl extends ServiceImpl<MateInventoryOtherOutLogMapper, MateInventoryOtherOutLog> implements MateInventoryOtherOutLogService {

    @Autowired
    private MateInventoryOtherOutLogMapper mateInventoryOtherOutLogMapper;

    @Autowired
    private MateInventoryOtherOutLogConverter mateInventoryOtherOutLogConverter;

    @Autowired
    private MateInventoryOtherOutLogDetailService mateInventoryOtherOutLogDetailService;

    @Autowired
    private SubWarehouseService subWarehouseService;

    @Autowired
    private MateInventoryService mateInventoryService;

    @Override
    public Page<MateInventoryOtherOutLogInfo> queryMateInventoryOtherOutLog(MateInventoryOtherOutLogQueryRequest request) {
        return mateInventoryOtherOutLogMapper.queryMateInventoryOtherOutLog(PageUtils.buildPage(request), request);
    }

    @Override
    public MateInventoryOtherOutLogDetailResponse getMateInventoryOtherOutLogById(Long mateInventoryOtherOutLogId) {
        MateInventoryOtherOutLogDetailResponse res = new MateInventoryOtherOutLogDetailResponse();
        MateInventoryOtherOutLogInfo main = mateInventoryOtherOutLogMapper.getMateInventoryOtherOutLogById(mateInventoryOtherOutLogId);
        // 查询仓库
        SubWarehouseInfo subWarehouseInfo = subWarehouseService.getSubWarehouseByCode(main.getSubWarehouseCode());
        main.setSubWarehouseName(subWarehouseInfo.getSubWarehouseName());
        res.setMain(main);
        List<MateInventoryOtherOutLogDetailInfo> item = mateInventoryOtherOutLogDetailService.getMateInventoryOtherOutLogDetailByOrderNo(main.getOrderNo());
        res.setItem(item);
        return res;
    }

    @Override
    public List<MateInventoryOtherOutLogInfo> listMateInventoryOtherOutLog(MateInventoryOtherOutLogQueryRequest request) {
        return mateInventoryOtherOutLogMapper.listMateInventoryOtherOutLog(request);
    }

    @Override
    public MateInventoryOtherOutLogAddResponse addMateInventoryOtherOutLog(MateInventoryOtherOutLogAddRequest request) {
        MateInventoryOtherOutLogAddResponse response = new MateInventoryOtherOutLogAddResponse();
        MateInventoryOtherOutLog mateInventoryOtherOutLog = new MateInventoryOtherOutLog();
        BeanUtils.copyProperties(request, mateInventoryOtherOutLog);
        // FIXME set默认字段
        mateInventoryOtherOutLogMapper.insert(mateInventoryOtherOutLog);
        response.setId(mateInventoryOtherOutLog.getId());
        return response;
    }

    @Override
    public MateInventoryOtherOutLogUpdateResponse updateMateInventoryOtherOutLog(MateInventoryOtherOutLogUpdateRequest request) {
        MateInventoryOtherOutLogUpdateResponse response = new MateInventoryOtherOutLogUpdateResponse();
        LambdaUpdateWrapper<MateInventoryOtherOutLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOtherOutLog::getId, request.getId());
        updateWrapper.set(MateInventoryOtherOutLog::getOrderNo, request.getOrderNo());
        updateWrapper.set(MateInventoryOtherOutLog::getSubWarehouseCode, request.getSubWarehouseCode());
        updateWrapper.set(MateInventoryOtherOutLog::getSaleSubChannel, request.getSaleSubChannel());
        updateWrapper.set(MateInventoryOtherOutLog::getOrderStatus, request.getOrderStatus());
        updateWrapper.set(MateInventoryOtherOutLog::getCreateUserCode, request.getCreateUserCode());
        updateWrapper.set(MateInventoryOtherOutLog::getCreateUserName, request.getCreateUserName());
        updateWrapper.set(MateInventoryOtherOutLog::getAuditUserCode, request.getAuditUserCode());
        updateWrapper.set(MateInventoryOtherOutLog::getAuditUserName, request.getAuditUserName());
        mateInventoryOtherOutLogMapper.update(null, updateWrapper);
        response.setId(request.getId());
        return response;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteMateInventoryOtherOutLog(Long mateInventoryOtherOutLogId) {
        MateInventoryOtherOutLog mateInventoryOtherOutLog = mateInventoryOtherOutLogMapper.selectById(mateInventoryOtherOutLogId);
        if (ObjectUtils.isEmpty(mateInventoryOtherOutLog)) {
            throw new RuntimeException("此单已被删除或不存在");
        }
        LambdaUpdateWrapper<MateInventoryOtherOutLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOtherOutLog::getId, mateInventoryOtherOutLogId);
        updateWrapper.set(MateInventoryOtherOutLog::getIsDeleted, true);
        int count = mateInventoryOtherOutLogMapper.update(null, updateWrapper);
        Boolean flag = mateInventoryOtherOutLogDetailService.deleteDetailByOrderNo(mateInventoryOtherOutLog.getOrderNo());
        if (!(count > 0 && flag)) {
            throw new RuntimeException("删除失败");
        }
        return true;
    }

    /**
     * 根据主键查询
     *
     * @param mateInventoryOtherOutLogId
     * @return
     */
    @Override
    public MateInventoryOtherOutLog selectMateInventoryOtherOutLogById(Long mateInventoryOtherOutLogId) {
        return mateInventoryOtherOutLogMapper.selectById(mateInventoryOtherOutLogId);
    }

    /**
     * 审核通过
     *
     * @param mateInventoryOtherOutLogId
     * @return
     */
    @Override
    public Boolean mateInventoryOtherOutLogPass(Long mateInventoryOtherOutLogId) {
        String currentUserCode = SPRequestContextHolder.getCurrentSystemUserLoginName();
        String currentUserName = SPRequestContextHolder.getCurrentSystemUserRealName();
        int count = mateInventoryOtherOutLogMapper.update(null,
                Wrappers.<MateInventoryOtherOutLog>lambdaUpdate().
                        eq(MateInventoryOtherOutLog::getId, mateInventoryOtherOutLogId)
                        .set(MateInventoryOtherOutLog::getOrderStatus, MateInventoryOtherOutLogOrderStatusEnum.S3.getIntValue())
                        .set(StringUtils.isNotEmpty(currentUserCode), MateInventoryOtherOutLog::getAuditUserCode, currentUserCode)
                        .set(StringUtils.isNotEmpty(currentUserName), MateInventoryOtherOutLog::getAuditUserName, currentUserName)
        );
        return count > 0;
    }

    /**
     * 审批拒绝
     *
     * @param mateInventoryOtherOutLogId
     * @return
     */
    @Override
    public Boolean mateInventoryOtherOutLogRefuse(Long mateInventoryOtherOutLogId) {
        String currentUserCode = SPRequestContextHolder.getCurrentSystemUserLoginName();
        String currentUserName = SPRequestContextHolder.getCurrentSystemUserRealName();
        int count = mateInventoryOtherOutLogMapper.update(null,
                Wrappers.<MateInventoryOtherOutLog>lambdaUpdate().
                        eq(MateInventoryOtherOutLog::getId, mateInventoryOtherOutLogId)
                        .set(MateInventoryOtherOutLog::getOrderStatus, MateInventoryOtherOutLogOrderStatusEnum.S1.getIntValue())
                        .set(StringUtils.isNotEmpty(currentUserCode), MateInventoryOtherOutLog::getAuditUserCode, currentUserCode)
                        .set(StringUtils.isNotEmpty(currentUserName), MateInventoryOtherOutLog::getAuditUserName, currentUserName)
        );
        return count > 0;
    }

    /**
     * 货品模式查询
     *
     * @param request
     * @return
     */
    @Override
    public Page<MateInventoryOtherOutLogModeInfo> queryMateInventoryOtherOutLogMode(MateInventoryOtherOutLogQueryRequest request) {
        return mateInventoryOtherOutLogMapper.queryMateInventoryOtherOutLogMode(PageUtils.buildPage(request), request);
    }

}
